// API配置
// 动态检测路径，适配nginx子系统部署
export const API_BASE_URL = (() => {
  // 开发环境使用localhost
  if (import.meta.env.DEV) {
    console.log('Development mode detected, using localhost API')
    return 'http://localhost:8081/api'
  }
  
  // 生产环境动态检测路径
  const currentPath = window.location.pathname
  console.log('Current path:', currentPath)
  
  // 如果在子系统下（如 /ota-system/），提取子系统路径
  if (currentPath !== '/' && currentPath !== '') {
    // 取第一段路径作为子系统路径
    const pathSegments = currentPath.split('/').filter(Boolean)
    console.log('Path segments:', pathSegments)
    if (pathSegments.length > 0) {
      const apiUrl = `/${pathSegments[0]}/api`
      console.log('Constructed API URL:', apiUrl)
      return apiUrl
    }
  }
  
  // 根系统情况
  console.log('Using root API URL: /api')
  return '/api'
})()

// API端点
export const API_ENDPOINTS = {
  // 认证管理
  AUTH_LOGIN: '/auth/login',
  
  // 产品管理
  PRODUCTS: '/products',
  PRODUCT: (id: string) => `/products/${id}`,
  
  // 固件管理
  FIRMWARES: '/firmwares',
  FIRMWARE: (id: string) => `/firmwares/${id}`,
  FIRMWARE_DOWNLOAD: (filename: string) => `/firmwares/download/${filename}`,
  
  // 任务管理
  TASKS: '/tasks',
  TASK: (id: string) => `/tasks/${id}`,
  
  // 设备管理
  DEVICES: '/devices',
  DEVICE: (id: string) => `/devices/${id}`,
  DEVICE_STATUS: (sn: string) => `/devices/status/${sn}`,
  
  // OTA服务
  OTA_CHECK: '/ota/check',
  OTA_DOWNLOAD: '/ota/download',
  OTA_REPORT: '/ota/report',
  
  // 升级记录
  UPGRADE_RECORDS: '/upgrade-records',
  
  // 健康检查
  HEALTH: '/health'
}

// HTTP状态码
export const HTTP_STATUS = {
  OK: 200,
  CREATED: 201,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  INTERNAL_SERVER_ERROR: 500
} 